package com.hjm.ui.common.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hjm.ui.R;

import java.util.List;
import java.util.Map;

public class HorizontalTextRecycleViewAdapter extends RecyclerView.Adapter<HorizontalTextRecycleViewAdapter.HorizontalTextRecycleViewAdapterHolder>  {
    private List<Map<String,String>> mData;
    private Listener listener;
    public HorizontalTextRecycleViewAdapter(List<Map<String,String>> mData) {
        this.mData = mData;
    }
    private RecyclerView recyclerView; // 添加一个成员变量来存储 RecyclerView 对象
    private int position = -1;

    @NonNull
    @Override
    public HorizontalTextRecycleViewAdapterHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal_text_recycle_view, parent, false);
        return new HorizontalTextRecycleViewAdapterHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HorizontalTextRecycleViewAdapterHolder holder, int position) {
        Map<String,String> map = mData.get(position);
        holder.textView.setText(map.get("text"));
        holder.textView.setTag(map.get("tag"));
        holder.textView.setOnClickListener((v -> {
            if (listener != null){
                setSelect(position);
                listener.onItemClick(v, position);
            }
        }));
        // 视图创建完成，调用回调方法，还要附加到窗口，中间有时间间隔，不能放这里
//        if (position == mData.size()-1)
//            listener.onViewCreated();
        this.position = position;
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull HorizontalTextRecycleViewAdapterHolder holder) {
        super.onViewDetachedFromWindow(holder);
        Log.d("横向文字列表适配器", "onViewAttachedToWindow: ViewHolder的视图（View）从RecyclerView的窗口（即屏幕上）分离时");
    }

    @Override
    public void onViewAttachedToWindow(@NonNull HorizontalTextRecycleViewAdapterHolder holder) {
        super.onViewAttachedToWindow(holder);
//        Log.d("横向文字列表适配器", "onViewAttachedToWindow: ViewHolder的视图被附加到RecyclerView的窗口");
        // 每个item附加到窗口上时都会调用此方法，只需要最后一个item附加到窗口上才调用回调函数
        if (position == mData.size()-1)
            listener.onViewCreated();
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public HorizontalTextRecycleViewAdapter(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
    }

    public void setData(List<Map<String,String>> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }

    class HorizontalTextRecycleViewAdapterHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public HorizontalTextRecycleViewAdapterHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text_view);
        }
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public interface Listener {
        void onItemClick(View view, int position);
        void onViewCreated();
    }

    /**
     * 将选中的设置为setSelected(true)，未选中的设置为setSelected(false)
     * @param position
     */
    public void setSelect(int position) {
        // 取消之前选中的 TextView 的选中状态
        for (int i = 0; i < getItemCount(); i++) {
            View itemView = recyclerView.getLayoutManager().findViewByPosition(i);
//            Log.d("横向文字列表适配器", "setSelect: 设置选中状态 当前组件位置 "+i+" 当前组件类型 "+itemView.getClass().getSimpleName());
            if (itemView instanceof LinearLayout) {
                TextView textView = itemView.findViewById(R.id.text_view);
                // 将所有的 TextView 设置为未选中状态
                textView.setSelected(false);
            }
        }
        // 将当前点击的 TextView 设置为选中状态
        View selectedView = recyclerView.getLayoutManager().findViewByPosition(position);
        if (selectedView instanceof LinearLayout) {
            TextView textView = selectedView.findViewById(R.id.text_view);
            Log.d("横向文字列表适配器", "setSelect: "+textView.getText()+" 设置为选中状态 "+position);
            textView.setSelected(true);
        }
    }
}
